/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.faction;

import javolution.util.FastList;

import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.custom.Message;
import com.l2jserver.gameserver.custom.MessageConfigManager;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;

/**
 * @author Laikeriz
 *
 */
public class Faction
{
	public int id = 0;
	public int nameColor = 0;
	public int circle = -1;
	public String name = "";
	public String stringNameColor = "";
	public FastList<String> playersOnline = new FastList<String>();
	
	public int[] baseCoords = new int[3];
	
	public Faction(int _id, String _name, int _circle, int nCol, String _stringNameColor, int[] bCoords)
	{
		id = _id;
		nameColor = nCol;
		circle = _circle;
		name = _name;
		baseCoords = bCoords;
		stringNameColor = _stringNameColor;
	}
	
	public void setPlayerInThis(final L2PcInstance player, boolean newChar)
	{
		player.setFaction(this);
		//		player.teleToLocation(getBaseCoords()[0], getBaseCoords()[1], getBaseCoords()[2]);
		ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
		{
			public void run()
			{
				if (player != null)
					player.teleToLocation(getBaseCoords()[0], getBaseCoords()[1], getBaseCoords()[2]);
			}
		}, 5000);
		handleAppearance(player);
		Message msg = MessageConfigManager.getMessage("successfullyChosen", player.langId);
		msg.addNextString(getName());
		player.sendMessage(msg.message);
		player.broadcastUserInfo();
		playersOnline.add(player.getName());
		if (newChar)
			player.storeCharBase();
	}
	
	public void handleAppearance(L2PcInstance player)
	{
		if (isWithCircles())
			player.setTeam(getCircle());
		if (isWithNameColor())
			player.getAppearance().setNameColor(getNameColor());
	}
	
	public void removePlayer(L2PcInstance player)
	{
		if (playersOnline.contains(player.getName()))
			playersOnline.remove(player.getName());
	}
	
	public void teleportToBase(L2PcInstance player)
	{
		player.teleToLocation(getBaseCoords()[0], getBaseCoords()[1], getBaseCoords()[2]);
	}
	
	public int getFactionId()
	{
		return id;
	}
	
	public int getNameColor()
	{
		return nameColor;
	}
	
	public int getCircle()
	{
		return circle;
	}
	
	public int[] getBaseCoords()
	{
		return baseCoords;
	}
	
	public String getName()
	{
		return name;
	}
	
	public boolean isWithCircles()
	{
		return circle > -1;
	}
	
	public boolean isWithNameColor()
	{
		return nameColor != -1;
	}
}
